<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .grid {
            margin: 100px auto;
            width: 500px;
            text-align: center;
        }
        
        .grid table {
            width: 100%;
            border-collapse: collapse;
        }
        
        .grid th,
        td {
            padding: 10px;
            border: 1px dashed orange;
            height: 35px;
        }
        
        .grid th {
            background-color: orange;
            border-top: none;
        }
        
        .book {
            height: 50px;
            background-color: orange;
            line-height: 50px;
        }
        
        .grid .total {
            height: 30px;
            line-height: 30px;
            background-color: orange;
            border: 1px dashed #c2d89a;
        }
    </style>
</head>

<body>
    <div id="app">
        <div class="grid">
            <div>
                <h1>图书管理</h1>
                <div class="book">
                    <div>
                        <label for="id">编号</label>
                        <input type="text" id='id' v-model='id' disabled>
                        <label for="name">名称</label>
                        <input type="text" id="name" v-model='name'>
                        <button @click='addData' :disabled='submitFlag'>提交</button>
                    </div>
                </div>
            </div>
            <div class="total">
                <span>图书总数：</span>
                <span>{{total}}</span>
            </div>
            <table>
                <thead>
                    <tr>
                        <th>编号</th>
                        <th>名称</th>
                        <th>时间</th>
                        <th>操作</th>
                    </tr>
                </thead>
                <tbody>
                    <tr :key='item.id' v-for='item in books'>
                        <td>{{item.id}}</td>
                        <td>{{item.name}}</td>
                        <td>{{item.date | formatTime(2)}}</td>
                        <td>
                            <a href="#" @click.prevent='modify(item.id)'>修改 </a>|
                            <a href="#" @click.prevent='deleteBook(item.id)'> 删除</a>
                        </td>
                    </tr>
                </tbody>
            </table>
        </div>
    </div>
    <script src="js/vue.js"></script>
    <script src="js/axios.js"></script>
    <script>
        axios.defaults.baseURL = 'http://localhost:3000/';
        axios.interceptors.response.use(res => {
            return res.data;
        }, err => {
            console.log(err);
        });
        //元素的补零计算
        function addZero(val) {
            if (val < 10) {
                return "0" + val;
            } else {
                return val;
            }
        };
        // console.log(addZero(9))
        //实现vue中的过滤器功能  先定义过滤器 在使用        value是过滤器前面的值，type是过滤器中定义的字段
        Vue.filter("formatTime", function(value, type) {
            var dataTime = "";
            var data = new Date();
            data.setTime(value);
            var year = data.getFullYear();
            var month = addZero(data.getMonth() + 1);
            var day = addZero(data.getDate());
            var hour = addZero(data.getHours());
            var minute = addZero(data.getMinutes());
            var second = addZero(data.getSeconds());
            if (type == 1) {
                dataTime = year + "-" + month + "-" + day;
            } else if (type == 2) {
                dataTime = year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second;
            } else if (type == 3) {
                dataTime = hour + ":" + minute + ":" + second;
            } else if (type == 4) {
                dataTime = year + "-" + month;
            }
            return dataTime; //将格式化后的字符串输出到前端显示
        });

        const vm = new Vue({
            el: '#app',
            data: {
                submitFlag: false,
                flag: false,
                id: '',
                name: '',
                books: []
            },
            methods: {
                allData: async function(data) {
                    this.books = await axios.get('books');
                },
                addData: async function() {

                    if (this.flag) {

                        var ret = await axios.put('books/' + this.id, {
                            name: this.name
                        })
                        if (ret.status == 200) {
                            this.allData();
                        }
                        this.flag = false;

                    } else {
                        let ret = await axios.post('books', {
                            name: this.name
                        })
                        if (ret.status == 200) {
                            this.allData();
                        }
                    }
                    this.id = '';
                    this.name = '';
                },
                modify: async function(id) {
                    console.log(id);
                    this.flag = true;
                    let ret = await axios.get('books/' + id);
                    this.id = ret.id;
                    this.name = ret.name;

                },
                deleteBook: async function(id) {
                    var ret = await axios.delete('books/' + id);
                    if (ret.status == 200) {
                        this.allData();
                    }
                }

            },
            computed: {
                total: function() {
                    return this.books.length;
                }
            },
            watch: {
                name: async function(val) {
                    var ret = await axios.get('books/book/' + this.name)
                    if (ret.status == 1) {
                        this.submitFlag = true;
                    } else {
                        this.submitFlag = false;
                    }
                }
            },
            mounted: function() {
                this.allData();
            }
        })
    </script>
</body>

</html>